package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qianniu.napi.admin.entity.DisplayFenlei;
import com.qianniu.napi.admin.entity.GoodsCategory;
import com.qianniu.napi.admin.service.IDisplayFenleiService;
import com.qianniu.napi.admin.service.IGoodsCategoryService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * <p>
 * </p>
 *
 * @author chenxuebin
 * @since 2019-03-10
 */
@Controller
public class DisplayFenleiController {

    private static final Logger logger = LoggerFactory.getLogger(DisplayFenleiController.class);

    @Autowired
    private IDisplayFenleiService displayfenleiService;
    @Autowired
    private IGoodsCategoryService goodscategoryService;
    @Autowired
    private IDisplayFenleiService displayFenleiService;

    @RequestMapping("admin/displayfenlei/index")
    public String index(Model model) {
        List<GoodsCategory> cates = goodscategoryService.selectList(new EntityWrapper<GoodsCategory>()
                .setSqlSelect("id,name,pic")
                .eq("mpid", ObjectUtil.getMpid())
                .orderBy("createtime", false)
        );
        model.addAttribute("cates", cates);

        List<DisplayFenlei> fenleis = new ArrayList<DisplayFenlei>();
        DisplayFenlei fenlei = new DisplayFenlei();
        fenlei.setShowname("请选择");
        fenleis.add(fenlei);
        fenleis.addAll(displayFenleiService.selectList(new EntityWrapper<DisplayFenlei>()
                .setSqlSelect("id,showname")
                .eq("state", 1)
                .eq("mpid", ObjectUtil.getMpid()).orderBy("sort")));
        model.addAttribute("fenleis", fenleis);

        return "admin/displayfenlei";
    }

    @RequestMapping("/admin/displayfenlei/list")
    @ResponseBody
    public Page<DisplayFenlei> list(@RequestParam(value = "offset", required = false) Integer offset,
                                    @RequestParam(value = "status", required = false) Integer status,
                                    @RequestParam(value = "search", required = false) String search,
                                    @RequestParam(value = "limit", required = false) Integer limit) {
        Wrapper<DisplayFenlei> wrapper = new EntityWrapper<DisplayFenlei>();
        wrapper.eq("mpid", ObjectUtil.getMpid());
//        if (!Strings.isNullOrEmpty(search))
//            wrapper.like("order_no", search).or().like("remark", search);

        wrapper.orderBy("sort");
        Page<DisplayFenlei> page = displayfenleiService.selectPage(new Page<DisplayFenlei>(offset / limit + 1, limit), wrapper);
        return page;
    }

    @RequestMapping("/admin/displayfenlei/add")
    public String add(@Valid DisplayFenlei bean,
                      BindingResult bindingResult, Model model) {
        // 字段校验
        bean.setCreatetime(new Date());
        bean.setUid(ObjectUtil.getUid());
        bean.setMid(ObjectUtil.getMid());
        bean.setMpid(ObjectUtil.getMpid());
        bean.setState(-1);
        if (bean.getListtype() == null) bean.setListtype(3);
        bean.insert();

        List<GoodsCategory> cates = goodscategoryService.selectList(new EntityWrapper<GoodsCategory>()
                .setSqlSelect("id,name,pic")
                .eq("mpid", ObjectUtil.getMpid())
                .orderBy("createtime", false)
        );
        model.addAttribute("cates", cates);

        List<DisplayFenlei> fenleis = new ArrayList<DisplayFenlei>();
        DisplayFenlei fenlei = new DisplayFenlei();
        fenlei.setShowname("请选择");
        fenleis.add(fenlei);
        fenleis.addAll(displayFenleiService.selectList(new EntityWrapper<DisplayFenlei>()
                .setSqlSelect("id,showname")
                .eq("state", 1)
                .eq("mpid", ObjectUtil.getMpid()).orderBy("sort")));
        model.addAttribute("fenleis", fenleis);

        return "admin/displayfenlei";
    }

    @RequestMapping("/admin/displayfenlei/getDisplayFenleiById")
    @ResponseBody
    public DisplayFenlei getDisplayFenleiById(long id) {
        DisplayFenlei displayfenlei = displayfenleiService.selectById(id);
        return displayfenlei;
    }

    @RequestMapping("/admin/displayfenlei/deleteOne")
    @ResponseBody
    public int deleteOne(long id) {
        boolean isSucc = displayfenleiService.deleteById(id);
        return isSucc ? 1 : 0;
    }

    @RequestMapping("/admin/displayfenlei/changeState")
    @ResponseBody
    public String changeState(long id, int state) {
        DisplayFenlei bean = new DisplayFenlei();
        bean.setId(id);
        bean.setState(state);
        bean.setModifytime(new Date());
        bean.setModifier(ObjectUtil.getUid());
        displayfenleiService.updateById(bean);
        return "success";
    }

    @RequestMapping("/admin/displayfenlei/modify")
    public String update(DisplayFenlei bean) {
        bean.setModifytime(new Date());
        bean.setModifier(ObjectUtil.getUid());
        boolean isSuccc = displayfenleiService.updateById(bean);
        if (isSuccc) {
            return "redirect:index";
        }
        return "error";
    }

    @RequestMapping("/admin/displayfenlei/moveSore")
    @ResponseBody
    public Boolean moveSore(long currendId, int currentSort, long moveId, int moveSort) {
        logger.info("moveSore");
        logger.info("currendId:" + currendId);
        logger.info("currentSort:" + currentSort);
        logger.info("moveId:" + moveId);
        logger.info("moveSort:" + moveSort);
        List<DisplayFenlei> navList = new ArrayList<DisplayFenlei>();

        DisplayFenlei moveNav = new DisplayFenlei();
        moveNav.setId(moveId);
        moveNav.setSort(currentSort);
        moveNav.setModifytime(new Date());
        moveNav.setModifier(ObjectUtil.getUid());
        //navService.updateById(moveNav);
        navList.add(moveNav);

        DisplayFenlei currentNav = new DisplayFenlei();
        currentNav.setId(currendId);
        currentNav.setSort(moveSort);
        currentNav.setModifytime(new Date());
        currentNav.setModifier(ObjectUtil.getUid());
        //navService.updateById(currentNav);
        navList.add(currentNav);

        return displayfenleiService.updateBatchById(navList);
    }

}
